package com.simon.customer.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.simon.customer.entity.DishType;
import com.simon.customer.entity.MerchantView;
import com.simon.customer.service.IService;
import com.simon.customer.service.impl.Service;

public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IService service = new Service();

	public MainServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// HttpSession session=request.getSession();

		//请求商家列表
		List<String> midlistReq=service.RequestMId();
		List<String> midlist=service.MidNotEmpty(midlistReq);//过滤掉所有没菜的商家
		
//		System.out.println(midlist);
		if(midlist.isEmpty()){
			request.getRequestDispatcher("NoMerchant.jsp").forward(request, response);
		}
		
		Map<String, DishType> type = new HashMap<>();
		List<DishType> list = service.showDishType(midlist);
		for (DishType dt : list) {
			type.put(dt.getTypeId(), dt);
		}
		request.setAttribute("type", type);
		//下拉菜品列表
		
		String dishType = request.getParameter("t_Id");
		if (dishType != null) {
			request.setAttribute("tId", dishType);	
			List<MerchantView> mer = service.showMerchantByType(dishType,midlist);
			Map<String, MerchantView> map = new HashMap<>();
			for (MerchantView m : mer) {
				map.put(m.getMerchantId(), m);
			}
			
			request.setAttribute("map", map);//某个菜品的商家列表
		} else {

		
			Map<String, MerchantView> map = new HashMap<>();
			for (String id : midlist) {
				map.put(id, service.showMerchant(id));
			}
			request.setAttribute("map", map);
		}//所有商家列表
		if(!midlist.isEmpty()){
		request.getRequestDispatcher("Customer.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
